home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: Does this create memory leak?...
- Date: Fri, 16 Feb 1996 13:16:39 +0100
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <312475A7.2781E494@intellektik.informatik.th-darmstadt.de>
- References: <3123DF68.1677@sierra.net>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
-
- T Colwell wrote:
- >
- > The code which follows seams like it would create a memory leak. The CAT class contains member pointers to
- > objects on the heap. When you take an existing object at reassign it via an overridden assignment operator to
- > a second object of the same type, what happens to the data at the originally pointed-to address? Here's what I
- > mean:
- >
- > *****************************************************
- > #include <iostreams.h>
- >
- > class CAT
- > {
- > public:
- > CAT(); // default constructor
- > // copy constructor and destructor intentionally excluded!
- > int GetAge() const { return *itsAge; }
- > int GetWeight() const { return *itsWeight; }
- > void SetAge(int age) { *itsAge = age; }
- > CAT operator=(const CAT &);
- >
- > private:
- > int *itsAge;
- > int *itsWeight;
- > };
- >
- > CAT::CAT()
- > {
- > itsAge = new int;
- > itsWeight = new int;
- > *itsAge = 5;
- > *itsWeight = 9;
- > }
- >
- > CAT CAT::operator=(const CAT & rhs)
- > {
- > if (this == &rhs)
- > return *this;
- > itsAge = new int;
- > itsWeight = new int;
- > *itsAge = rhs.GetAge();
- > *itsWeight = rhs.GetWeight();
- > }
- >
- > void main()
- > {
- > CAT frisky;
- > frisky.SetAge(6);
- > CAT whiskers;
- > whiskers = frisky; // <--doesn't this leave
- > // the old members of whiskers
- > // stranded in the heap?
- > }
- > **************************************************
- >
- > The assignment operator reinitializes itsAge and itsWeight to point to new heap addresses, what happens to the
- > addresses they were pointing to before? Doesn't this create stranded addresses (leaks)?
- >
-
- This code will produce a memory leak whenever you create an
- instance of class 'CAT'. Usually one needs a copy-ctor, dtor
- and assignment-operator if the ctor isn't trivial.
- For instance the following code outlines a possible implementation
- >>>>
- #include <iostream.h>
-
- class CAT {
- public:
- CAT() : itsAge(new int(5)), itsWeight(new int(9)) {}
- CAT(const CAT& cat) { Copy(cat); }
- ~CAT() { Destroy(); }
- CAT& operator=(const CAT & cat) {
- if (this!=&cat) { Destroy(); Copy(cat); }
- return *this;
- }
- void SetAge(int age) { *itsAge = age; }
-
- private:
- int *itsAge;
- int *itsWeight;
-
- void Destroy() { delete itsAge; delete itsWeight; }
- void Copy(const CAT& cat) {
- itsAge=new int(*cat.itsAge); itsWeight=new int(*cat.itsWeight);
- }
- };
-
- int main()
- {
- CAT frisky;
- frisky.SetAge(6);
- CAT whiskers;
- whiskers = frisky;
-
- return 0;
- }
- <<<<
- Some notes:
- ctor: The member-initializer list is used to initialize the
- dynamicly maintained integer values.
- copy-ctor: The copy-construction is delegated to the private function 'Copy'.
- dtor: Again a private function is used to release the allocated
- ressources.
- ass-op: The assignment-operator can now be easily expressed in terms
- of destruction ('Destroy') and copy-construction ('Copy').
-
- This guarantees that the class has 'proper copy-semantics'.
-
- Enno
-